import java.util.List;
import java.util.Map;

/**
 * Created by thoughtworker on 1/16/14.
 */
public class LeagueManager {

    private Map<String, Player> playerDict;
    private IOHandler ioHandler;
    private List<Coach> coachList;

    public LeagueManager(IOHandler ioHandler, Map<String, Player> playerDict, List<Coach> coachList) {
        this.ioHandler = ioHandler;
        this.playerDict = playerDict;
        this.coachList = coachList;
    }

    public void run(){
        String input = "default";
        ioHandler.printWelcome();
        while (true){
            ioHandler.printMainMenu();
            input = ioHandler.promptAndReceiveMenuInput();
            if (handleInputAndCheckForExit(input)) break;
        }
    }

    private boolean handleInputAndCheckForExit(String input) {
        if (input.equals("1")){
            printLeagueMembers();
        }
        else if (input.equals("2")){
            findPlayer();
        }
        else if (input.equals("3")){
            printTeamMembers();
        }
        else if (input.equals("exit")){
            return true;
        }
        else{
            ioHandler.printInvalidMenuInput();
        }
        return false;
    }

    private void printTeamMembers() {
        int numberOnTeam = 0;
        String target = ioHandler.promptAndGetTeamName();
        for(Player player:playerDict.values()){
            if (player.getTeam().equals(target)){
                ioHandler.printPlayerDetails(player);
                numberOnTeam++;
            }
        }
        for(Coach coach:coachList){
            if (coach.getTeam().equals(target)){
                ioHandler.printCoachDetails(coach);
                numberOnTeam++;
            }
        }
        if (numberOnTeam == 0){
            ioHandler.printTeamNotInLeague();
        }
        System.out.println();
    }

    public void printLeagueMembers() {
        ioHandler.printPlayers(playerDict);
        ioHandler.printCoaches(coachList);
    }

    public void findPlayer(){
        boolean keepGoing = false;
        do {
            findAndPrintPlayer();
            keepGoing = checkToContinue();
        }while(keepGoing);
    }

    private void findAndPrintPlayer() {
        String name = ioHandler.promptAndReceivePlayerNameSearch();
        String number = ioHandler.promptAndReceivePlayerNumberSearch();
        Player targetPlayer = new Player(name,"",number,0);
        if(playerDict.containsValue(targetPlayer)){
            ioHandler.printPlayerDetails(playerDict.get(name));
        }
        else{
            ioHandler.playerNotFound();
        }
    }

    private boolean checkToContinue() {
        boolean keepGoing;
        while(true){
            ioHandler.printContinue();
            String input = ioHandler.promptAndReceiveMenuInput();
            if(input.equals("n")){
                keepGoing = false;
                break;
            }
            else if(input.equals("y")){
                keepGoing = true;
                break;
            }
            else{
                ioHandler.printInvalidMenuInput();
            }
        }
        return keepGoing;
    }
}
